/*
 * Created on 30-Aug-2004
 *
 * By Mike Anderson
 */
package game.jrogue;

import java.util.*;


public class Gods {
	public static final String YANTHRALL="Yanthrall";
	public static final String ARVALLON="Arvallon";
	public static final String ARAMIS="Aramis";
	public static final String DURVAL="Durval";	
	public static final String ZUROCH="Zuroch";	
	public static final String MOLKOTH="Molkoth";	
	public static final String BZAKKARATH="B'Zakkarath";	
	public static final String TROTHGAR="Trothgar";	
	public static final String NASSARRIK="Nassarrik";	
	public static final String GATHAARL="Gathaarl";
	
	public static Thing get(String s) {
		HashMap h=getGods();
		Thing t=(s==null)?null:(Thing)h.get(s);
		if (t==null) Game.warn(Text.krSub("["+s+"]", "라")+"는 신은 존재하지 않습니다!");
		return t;
	}
	
	public static Thing getGod(Thing being) {
		return get(getGodName(being));
	}
	
	public static String getGodName(Thing being) {
		return being.getString("종교");
	}
	
	public static HashMap getGods() {
		HashMap h=(HashMap)Lib.instance().getObject("신들");
		if (h==null) {
			h=new HashMap();
            Lib.instance().set("신들", h);
//			Game.hero.set("신들",h);
		}
		return h;
	}
	
	/**
	 * Gets the possible list of gods for a given character
	 * 
	 * @param h
	 * @return
	 */
	public static ArrayList getPossibleGods(Thing h) {
		ArrayList al=new ArrayList();
		
		Iterator it=getGods().keySet().iterator();
		while(it.hasNext()) {
			String s=(String)it.next();
			if (acceptsFollower(s,h)) al.add(s);
		}
		
		return al;
	}
	
	public static void sacrifice(Thing hero, Thing sac) {
		hero.incStat("APS",-100);
		sac.remove();
		
		Thing god=getGod(hero);
		int value=Item.value(sac);
		Game.message(sac.getTheName()+" "+sac.is()+" "+god.getString("제물설명"));
		
		// modify by level difference
		// exact level=100% value
		// each additional level=+20% value
		int heroLevel=hero.getLevel();
		int ld=sac.getLevel()-hero.getLevel();
		ld=RPG.max(0,ld);
		value=(int)(value*(1.0+0.2*ld));
		
		double relativeValue=value/Item.levelValue(heroLevel);
		int points=0;
		if (relativeValue>1.0) points++;
		if (relativeValue>10.0) points++;
		if (relativeValue>100.0) points++;
		if (relativeValue>1000.0) points++;
		if (relativeValue>10000.0) points++;
		
		// See whether god likes this particular sacrifice
		String likes=god.getString("선호제물");
		boolean like=false;
		String[] ss=likes.split(",");
		for (int i=0; i<ss.length; i++) {
			if (sac.getFlag(ss[i])) like=true;
		}
		if (like) {
			points*=3;
		}
		
		if (sac.getFlag("IsArtifact")) {
			Game.message("\"그대의 깊은 신앙은 보답을 받을 것입니다.\"");
			hero.incStat(RPG.ST_FATE,1);
			if (!hero.getFlag(Skill.PRAYER)) {
				hero.incStat(Skill.PRAYER,1);
			}
		} else if (points>0) {
			Gods.impress(hero,points);
		} else if (points==0) {
			Game.message("당신은 "+god.name()+" 신이 당신의 허접한 제물에 별로 감흥을 받지 않았음을 느낍니다");
			
		} else if (points<0) {
			Gods.anger(hero,-points);
		}
	}
	
	public static boolean acceptsFollower(String god, Thing h) {
		Thing g=get(god);
		boolean accepts=true;
		
		String ar=g.getString("선택가능종족");
		String r=h.getString("종족");
		if (ar.equals("모두")||(ar.indexOf(r)>=0)) {
			accepts&=true;
		} else {
			accepts=false;
			//Game.warn(god+" rejects due to race ["+r+"]");
		}
		
		String ap=g.getString("선택가능직업");
		String p=h.getString("직업");
		if (ap.equals("모두")||(ap.indexOf(p)>=0)) {
			accepts&=true;
		} else {
			accepts=false;
			//Game.warn(god+" rejects due to profession ["+r+"]");
		}
		
		int ad=g.getStat("선택가능가치관차이");
		if (Math.abs(getGoodness(h)-getGoodness(g))>ad) {
			accepts=false;
			//Game.warn(god+" rejects due to alignment ["+r+"]");

		}
		
		return accepts;
	}
	
	private static void addGod(Thing t) {
		getGods().put(t.name(),t);
		Lib.add(t);
		// Game.warn("Adding god - "+t.name());
	}
	
	public static int getGoodness(Thing t) {
		String al=t.getString("도덕성향");
		char base=al.charAt(0);
		int g=0;
		if (base=='G') g+=3;
		else if (base=='E') g-=3;
		
		if (al.length()>1) {
			char mod=al.charAt(1);
			if (mod=='+') g+=1;
			else if (mod=='-') g-=1;
		}
		
		return g;
	}
	
	public static String getAlignment(int goodness) {
		switch(goodness) {
			case -3: return "E";
			case -2: return "E+";
			case -1: return "N-";
			case 0: return "N";
			case 1: return "N+";
			case 2: return "G-";
			case 3: return "G";
		}
		if (goodness>0) return "G+";
		return "E-";
	}
	
	/**
	 * Call when a being does something that angers his/her god
	 */ 
	public static boolean anger(Thing b, int amount) {
		if (amount<0) amount=0;

		b.incStat("죄",amount);
		
		if (amount>0) {
			b.message("당신은 "+getGodName(b)+" 신이 화를 내는 것을 느낍니다");
		}
		
		return (amount>0);
	}

	/**
	 * Call when a being does something that angers his/her god
	 */ 
	public static boolean impress(Thing b, int amount) {
		if (amount<0) return anger(b,-amount);
		
		b.incStat("신앙",amount);
		
		if (amount>0) {
			b.message("당신은 "+getGodName(b)+" 신이 당신의 행동에 기뻐하는 것을 느낍니다");
		}
		
		return (amount>0);
	}
	
	public static void checkEat(Thing b, Thing food) {
		Thing god=Gods.getGod(b);
		if (!Food.isVegetarian(food)) {
			int veggie=god.getStat("초식주의자전용");
			if (veggie>0) {
				anger(b,veggie);
			}
		}
	}
	
	public static int checkTheft(Thing b) {
		Thing god=getGod(b);
		int goodness=getGoodness(god);
		int likesTheft=god.getStat("절도선호");
		
		int amount=0;
		if (goodness>=0) {
			amount=-(goodness+1)*(goodness+2)/2;
		} else if (likesTheft>0) {
			amount=likesTheft;
		} else if (goodness<=-4) {
			amount=1;
		} 
		
		Gods.impress(b,amount);
		return amount;
	}
	
	public static void init() {
		Thing t;
		
		t=Lib.extend("기본 신","기본 사물");
		t.set("선택가능가치관차이",2);
		t.set("제물설명","불타올라 사라져 버립니다");
		t.set("선호제물",null);
		t.set("최소레벨",1);
		Lib.add(t);
		
		t=Lib.extend(ARAMIS,"기본 신");
		t.set("신의칭호","치유의 신");
		t.set("선택가능종족","모두");
		t.set("선호제물","IsArmour,IsPotion");
		t.set("선택가능직업","순찰자,사제,주술사,치유사,성기사");
		t.set("제물설명","화려한  빛 속으로 사라집니다");
		t.set("도덕성향","G+");
		t.set("UpbringingText","In accordance with the teachings of Aramis, you learnt to respect all living things and in particular never to eat the flesh of animals.");
		t.set("초식주의자전용",1);
		addGod(t);
		
		t=Lib.extend(DURVAL,"기본 신");
		t.set("신의칭호","산맥의 신");
		t.set("선택가능종족","호빗,드워프,노움,하프 트롤,아르고니언,호큰");
		t.set("선택가능직업","모두");
		t.set("선호제물","IsCoin,IsWeapon");
		t.set("UpbringingText","You learnt of the secret treasures buried in the Deep, which you should always strive to acquire to the greater glory of Durval.");
		t.set("도덕성향","N+");
		addGod(t);
		
		t=Lib.extend(ARVALLON,"기본 신");
		t.set("신의칭호","광휘의 선한 신");
		t.set("선택가능종족","인간,하이 엘프,우드 엘프,펜사도리언");
		t.set("선택가능직업","모두");
		t.set("선호제물","IsScroll,IsBook");
		t.set("제물설명","눈을 뜰 수 없을정도로 밝은 빛 속으로 사라집니다");
		t.set("UpbringingText","You learnt of how Arvallon alone was granted the key to all knowledge by the Creator, and would bestow the greatest of secrets upon His loyal followers.");
		t.set("도덕성향","G");
		addGod(t);
		
		t=Lib.extend(YANTHRALL,"기본 신");
		t.set("신의칭호","데이도르 제국의 전쟁신");
		t.set("선택가능종족","인간,우드 엘프,호빗,드워프,노움");
		t.set("선택가능직업","모두");
		t.set("선호제물","IsWeapon,IsArmour");
		t.set("제물설명","거대한 불의 기둥 속으로 삼켜져 사라집니다");
		t.set("UpbringingText","As with all followers of Yanthrall, you came to appreciate that life is a ceaseless battle, and that victory belongs to The Strong and The Just. You dreamed of one day being initiated into the mighty ranks of the Blood Guard.");
		t.set("도덕성향","N+");
		addGod(t);
		
		t=Lib.extend(ZUROCH,"기본 신");
		t.set("신의칭호","풍요의 신");
		t.set("선택가능종족","모두");
		t.set("선택가능직업","모두");
		t.set("선택가능가치관차이",3);
		t.set("제물설명","어디론가 사라져 버립니다");
		t.set("선호제물","IsCoin,IsFood");
		t.set("UpbringingText","You heeded well the command of Zuroch, that His followers should seek to find joy in the accumulation of wealth and the making of sizeable donations to temple coffers.");
		t.set("도덕성향","N-");
		addGod(t);
		
		t=Lib.extend(MOLKOTH,"기본 신");
		t.set("신의칭호","저승의 암흑신");
		t.set("선택가능종족","인간,노움,다크 엘프,하프 트롤,하프 오크,펜사도리언");
		t.set("선택가능직업","모두");
		t.set("선택가능가치관차이",3);
		t.set("선호제물","IsPotion,IsWeapon");
		t.set("제물설명","끔찍한 검은 불길 속으로 불타 사라져버립니다");
		t.set("UpbringingText","You learnt that all existence will one day be swallowed by Darkness, when Molkoth and His invincible demonic armies of destruction sweep to victory aginst the pathetic Forces of Light. Only those who serve Molkoth will be granted a place in the Afterworld, with the souls of all others destined for eternal pain.");
		t.set("도덕성향","E-");
		addGod(t);
		
		t=Lib.extend(BZAKKARATH,"기본 신");
		t.set("신의칭호","저승의 혼돈신");
		t.set("선택가능종족","모두");
		t.set("선택가능직업","모두");
		t.set("선호제물","IsWeapon,IsGem");
		t.set("UpbringingText","You learnt to see beauty in mayhem and destruction, pleasure in pain and rejoice in the relentless spread of Chaos.");
		t.set("도덕성향","E");
		addGod(t);
		
		t=Lib.extend(TROTHGAR,"기본 신");
		t.set("신의칭호","God of The Wild");
		t.set("선택가능종족","인간,드워프,다크 엘프,하프 오크,하프 트롤,아르고니언,호큰");
		t.set("선택가능직업","야만전사,도적,전사,순찰자,주술사");
		t.set("선호제물","IsFood,IsPotion");
		t.set("UpbringingText","You learnt of the colossal power of the Spirits of Nature, and that you should offer sacrifices to Trothgar to earn His protection.");
		t.set("도덕성향","N-");
		addGod(t);
		
		t=Lib.extend(NASSARRIK,"기본 신");
		t.set("신의칭호","God of Thieves");
		t.set("선택가능종족","모두");
		t.set("선택가능직업","야만전사,도적");
		t.set("절도선호",1);
		t.set("선호제물","IsCoin,IsPotion");
		t.set("UpbringingText","You learn of the dark cults that pervade the lands of men, and how you should seek to serve them as a loyal follower of Nassarrik.");
		t.set("도덕성향","E+");
		addGod(t);
		
		t=Lib.extend(GATHAARL,"기본 신");
		t.set("신의칭호","고블린 전투의 신");
		t.set("선택가능종족","하프 오크,하프 트롤");
		t.set("선택가능직업","모두");
		t.set("선호제물","IsFood,IsWeapon");
		t.set("제물설명","녹색 연기 속으로 녹아 사라져버립니다");
		t.set("UpbringingText","You learn to rejoice in the thrill of battle, that you should multiply to cover the earth, and that you should raise great totems to the glory of Gathaarl and the Goblin Spirits.");
		t.set("도덕성향","N-");
		addGod(t);
		
	}
}
